package belajarzk.action.report;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import net.sf.jasperreports.engine.JRAbstractExporter;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRMapCollectionDataSource;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.export.JRRtfExporter;
import net.sf.jasperreports.engine.export.JRXlsExporter;
import net.sf.jasperreports.engine.export.JRXlsExporterParameter;

import org.zkoss.util.media.AMedia;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.WebApp;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.util.GenericAutowireComposer;
import org.zkoss.zul.Filedownload;

import belajarzk.common.Common;
import belajarzk.database.hibernate.HibernateUtil;


public class Report extends GenericAutowireComposer {

	/**
	 * 
	 */

	public static String PDF = "pdf";
	public static String XLS = "xls";
	public static String RTF = "rtf";

	private static final long serialVersionUID = -7955063459194204077L;

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		// TODO Auto-generated method stub
		super.doAfterCompose(comp);

	}

//	@SuppressWarnings( { "unchecked" })
//	public static void generatePDFReport(String formatLaporan, Map parameters,
//			String file, Date t, List<Map<String, Object>> maps,
//			WebApp application) {
//		parameters.put("logo", application.getRealPath("/report/logo.png"));
//		parameters.put("REPORT_LOCALE", Common.locale);
//		JRMapCollectionDataSource dataSource = new JRMapCollectionDataSource(
//				maps);
//		JasperPrint jasperPrint = null;
//		ByteArrayOutputStream outputReport = null;
//		try {
//			jasperPrint = JasperFillManager.fillReport(application
//					.getRealPath("/report/" + file + ".jasper"), parameters,
//					dataSource);
//
//			outputReport = new ByteArrayOutputStream();
//
//			JRAbstractExporter exporterPDF;
//			if (formatLaporan.equals(PDF)) {
//				exporterPDF = new JRPdfExporter();
//			} else if (formatLaporan.equals(XLS)) {
//				exporterPDF = new JRXlsExporter();
//			} else if (formatLaporan.equals(RTF)) {
//				exporterPDF = new JRRtfExporter();
//			} else {
//				exporterPDF = new JRPdfExporter();
//			}
//			exporterPDF.setParameter(JRXlsExporterParameter.JASPER_PRINT,
//					jasperPrint);
//			exporterPDF.setParameter(JRXlsExporterParameter.OUTPUT_STREAM,
//					outputReport);
//			exporterPDF
//					.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET,
//							Boolean.FALSE);
//			exporterPDF.setParameter(
//					JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND,
//					Boolean.TRUE);
//			exporterPDF.setParameter(
//					JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,
//					Boolean.TRUE);
//			exporterPDF.exportReport();
//		} catch (JRException e) {
//			e.printStackTrace();
//		}
//		byte[] data = outputReport.toByteArray();
//
//		// prepare the AMedia for iframe
//		final InputStream medibnn = new ByteArrayInputStream(data);
//		String currentDateTime = new SimpleDateFormat("dd_MMMMM_yyyy",
//				Common.locale).format(t);
//		final AMedia amedia = new AMedia(file + "_" + currentDateTime + "."
//				+ formatLaporan, "pdf", "application/" + formatLaporan, medibnn);
//
//		Filedownload.save(amedia);
//	}
	

	@SuppressWarnings( { "unchecked" })
	public static void generatePDFReport(String formatLaporan, Map parameters,
			String file, Date t, List<Map<String, Object>> maps,
			WebApp application) {
		parameters.put("logo", application.getRealPath("/report/logo.png"));
		parameters.put("REPORT_LOCALE", Common.locale);
		JRMapCollectionDataSource dataSource = new JRMapCollectionDataSource(
				maps);
		JasperPrint jasperPrint = null;
		ByteArrayOutputStream outputReport = null;
		System.out.println("file = " + file);
		try {
			jasperPrint = JasperFillManager.fillReport(application
					.getRealPath("/report/" + file + ".jasper"), parameters,
					dataSource);

			outputReport = new ByteArrayOutputStream();

			JRAbstractExporter exporterPDF;
			if (formatLaporan.equals(PDF)) {
				exporterPDF = new JRPdfExporter();
			} else if (formatLaporan.equals(XLS)) {
				exporterPDF = new JRXlsExporter();
			} else if (formatLaporan.equals(RTF)) {
				exporterPDF = new JRRtfExporter();
			} else {
				exporterPDF = new JRPdfExporter();
			}
			exporterPDF.setParameter(JRXlsExporterParameter.JASPER_PRINT,
					jasperPrint);
			exporterPDF.setParameter(JRXlsExporterParameter.OUTPUT_STREAM,
					outputReport);
			exporterPDF
					.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET,
							Boolean.FALSE);
			exporterPDF.setParameter(
					JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND,
					Boolean.TRUE);
			exporterPDF.setParameter(
					JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,
					Boolean.TRUE);
			exporterPDF.exportReport();
		} catch (JRException e) {
			e.printStackTrace();
		}
		byte[] data = outputReport.toByteArray();

		// prepare the AMedia for iframe
		final InputStream mediais = new ByteArrayInputStream(data);
		String currentDateTime = new SimpleDateFormat("dd_MMMMM_yyyy",
				Common.locale).format(t);
		final AMedia amedia = new AMedia(file + "_" + currentDateTime + "."
				+ formatLaporan, "pdf", "application/" + formatLaporan, mediais);

		Filedownload.save(amedia);
	}

//	@SuppressWarnings( { "deprecation", "unchecked" })
//	public static void generatePDFReport(String formatLaporan, Map parameters,
//			String file, Date t, WebApp application) {
//
//		parameters.put("logo", application.getRealPath("/report/logo.png"));
//		parameters.put("REPORT_LOCALE", Common.locale);
//		Connection conn = HibernateUtil.currentSession()
//				.connection();
//
//		System.out.println("file = " + file);
//		JasperPrint jasperPrint = null;
//		ByteArrayOutputStream outputReport = null;
//		try {
//			jasperPrint = JasperFillManager.fillReport(application
//					.getRealPath("/report/" + file + ".jasper"), parameters,
//					conn);
//			outputReport = new ByteArrayOutputStream();
//
//			JRAbstractExporter exporterPDF;
//			if (formatLaporan.equals(PDF)) {
//				exporterPDF = new JRPdfExporter();
//			} else if (formatLaporan.equals(XLS)) {
//				exporterPDF = new JRXlsExporter();
//			} else if (formatLaporan.equals(RTF)) {
//				exporterPDF = new JRRtfExporter();
//			} else {
//				exporterPDF = new JRPdfExporter();
//			}
//			exporterPDF.setParameter(JRXlsExporterParameter.JASPER_PRINT,
//					jasperPrint);
//			exporterPDF.setParameter(JRXlsExporterParameter.OUTPUT_STREAM,
//					outputReport);
//			exporterPDF
//					.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET,
//							Boolean.FALSE);
//			exporterPDF.setParameter(
//					JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND,
//					Boolean.TRUE);
//			exporterPDF.setParameter(
//					JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,
//					Boolean.TRUE);
//			exporterPDF.exportReport();
//		} catch (JRException e) {
//			e.printStackTrace();
//		}
//		byte[] data = outputReport.toByteArray();
//
//		// prepare the AMedia for iframe
//		final InputStream mediais = new ByteArrayInputStream(data);
//		String currentDateTime = new SimpleDateFormat("dd_MMMMM_yyyy",
//				Common.locale).format(t);
//		final AMedia amedia = new AMedia(file + "_" + currentDateTime + "."
//				+ formatLaporan, "pdf", "application/" + formatLaporan, mediais);
//
//		Filedownload.save(amedia);
//	}

	@SuppressWarnings( { "deprecation", "unchecked" })
	public static void generatePDFReport(String formatLaporan, Map parameters,
			String file, Date t, WebApp application) {

		parameters.put("logo", application.getRealPath("/report/logo.png"));
		parameters.put("REPORT_LOCALE", Common.locale);
		Connection conn = HibernateUtil.currentSession()
				.connection();
		JasperPrint jasperPrint = null;
		ByteArrayOutputStream outputReport = null;
		try {
			jasperPrint = JasperFillManager.fillReport(application
					.getRealPath("/report/" + file + ".jasper"), parameters,
					conn);
			outputReport = new ByteArrayOutputStream();

			JRAbstractExporter exporterPDF;
			if (formatLaporan.equals(PDF)) {
				exporterPDF = new JRPdfExporter();
			} else if (formatLaporan.equals(XLS)) {
				exporterPDF = new JRXlsExporter();
			} else if (formatLaporan.equals(RTF)) {
				exporterPDF = new JRRtfExporter();
			} else {
				exporterPDF = new JRPdfExporter();
			}
			exporterPDF.setParameter(JRXlsExporterParameter.JASPER_PRINT,
					jasperPrint);
			exporterPDF.setParameter(JRXlsExporterParameter.OUTPUT_STREAM,
					outputReport);
			exporterPDF
					.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET,
							Boolean.FALSE);
			exporterPDF.setParameter(
					JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND,
					Boolean.TRUE);
			exporterPDF.setParameter(
					JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,
					Boolean.TRUE);
			exporterPDF.exportReport();
		} catch (JRException e) {
			e.printStackTrace();
		}
		byte[] data = outputReport.toByteArray();

		// prepare the AMedia for iframe
		final InputStream medibnn = new ByteArrayInputStream(data);
		String currentDateTime = new SimpleDateFormat("dd_MMMMM_yyyy",
				Common.locale).format(t);
		final AMedia amedia = new AMedia(file + "_" + currentDateTime + "."
				+ formatLaporan, "pdf", "application/" + formatLaporan, medibnn);

		Filedownload.save(amedia);
	}

	@SuppressWarnings( { "deprecation", "unchecked" })
	public static String generatePDFFileReport(String formatLaporan,
			Map parameters, String file, Date t, WebApp application)
			throws Exception {

		parameters.put("logo", application.getRealPath("/report/logo.png"));
		parameters.put("REPORT_LOCALE", Common.locale);
		Connection conn = HibernateUtil.currentSession()
				.connection();
		JasperPrint jasperPrint = null;
		ByteArrayOutputStream outputReport = null;
		try {
			jasperPrint = JasperFillManager.fillReport(application
					.getRealPath("/report/" + file + ".jasper"), parameters,
					conn);
			outputReport = new ByteArrayOutputStream();

			JRAbstractExporter exporterPDF;
			if (formatLaporan.equals(PDF)) {
				exporterPDF = new JRPdfExporter();
			} else if (formatLaporan.equals(XLS)) {
				exporterPDF = new JRXlsExporter();
			} else if (formatLaporan.equals(RTF)) {
				exporterPDF = new JRRtfExporter();
			} else {
				exporterPDF = new JRPdfExporter();
			}
			exporterPDF.setParameter(JRXlsExporterParameter.JASPER_PRINT,
					jasperPrint);
			exporterPDF.setParameter(JRXlsExporterParameter.OUTPUT_STREAM,
					outputReport);
			exporterPDF
					.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET,
							Boolean.FALSE);
			exporterPDF.setParameter(
					JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND,
					Boolean.TRUE);
			exporterPDF.setParameter(
					JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,
					Boolean.TRUE);
			exporterPDF.exportReport();
		} catch (JRException e) {
			e.printStackTrace();
		}
		byte[] data = outputReport.toByteArray();

		// prepare the AMedia for iframe
		final InputStream medibnn = new ByteArrayInputStream(data);
		String currentDateTime = new SimpleDateFormat("dd_MMMMM_yyyy_HH_mm_ss",
				Common.locale).format(t);
		final File myfile = new File(application.getRealPath("/report/temp")
				+ "/" + file + "_" + currentDateTime + "." + formatLaporan);
		System.out.println("file = " + myfile.getAbsolutePath());
		myfile.getParentFile().mkdirs();
		myfile.createNewFile();
		FileOutputStream stream = new FileOutputStream(myfile);
		int c;

		while ((c = medibnn.read()) != -1)
			stream.write(c);
		medibnn.close();
		stream.close();
		return myfile.getName();
	}

	public void onBantuan(Event event) throws Exception {
		File file = new File(
				application
						.getRealPath("/help/User_Manual__Sistem_Informasi_Akademik.pdf"));
		System.out.println("file = " + file);
		final InputStream medibnn = new FileInputStream(file);
		final AMedia amedia = new AMedia(
				"Bantuan_Sistem_Informasi_Akademik.pdf", "pdf",
				"application/pdf", medibnn);
		Filedownload.save(amedia);
	}

}
